cap log close

global path = "****"

local name= "mort_figures"  
local date = "051920"
global logname = "$path/`name'_`date'.log"

clear
set more off
cd "$path"
log using "$logname", replace

use "$path/Data/penalty_sample.dta", clear


*define sample
keep if age_2017<65 & age_2017>=45

*month of death
gen month_n = (yod-2016)*12 + mod

drop if month_n<=0 // these people died before 2016


*construct counts
forval m=1/36 {
	gen dead_by_`m'=month_n<=`m'
}



*mortality variables for six month interval 
forval m=6(6)36 {
gen dead_int_`m' = ( month_n <=`m' & month_n >= `m'- 5  )
}

*regession estimate for cumulative mortality
cap erase "$path/mort_by_month_`date'.ster"

foreach x of numlist 1(6)31 36{
reg dead_by_`x' treatment if notall16 == 1 , cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/mort_by_month_`date'.ster", append
}

*regession estimate for mortality in six month interval
cap erase "$path/mort_interval_`date'.ster"

forval m=6(6)36{
reg dead_int_`m' treatment if notall16 == 1 , cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/mort_interval_`date'.ster", append
}


local varlist = "" 
forval m=1/36 {
	local varlist = "`varlist' dead_by_`m'"
}


*collapse
collapse (count) dep_tin (sum) `varlist' ,by(treatment notall16)

save "$path/mortality_by_month_`date'.dta", replace


log close

